package ui.views;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.LayoutStyle.ComponentPlacement;
import drinks.Beverage;

import javax.swing.border.LineBorder;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;

public class UIChoose extends JFrame {

	private static final long serialVersionUID = -5776326676252109157L;
	private JPanel contentPane;
	private Dimension dimension;

	/**
	 * class's singleton
	 */
	private static UIChoose uiChooseInstance;

	private JPanel greetingPanel;
	private JPanel creditPanel;
	private JPanel poweredPanel;
	private JLabel label;
	private JLabel lblWelcome;
	private JPanel selectionPanel;
	private JComboBox<Beverage> beveragesComboBox;
	private JTextField amountTextBox;
	private JLabel amountLabel;
	private JLabel beverageLabel;
	private JPanel addPanel;
	private JButton addButton;
	private JLabel label_1;
	private JLabel label_2;
	private JPanel displayBeveragePanel;
	private JPanel removePanel;
	private JButton removeButton;
	private JTable selectedBeverageTable;
	private JTextField txtBeverageAmountPrice;
	private Vector<Object> vectorInsertToTable = new Vector<Object>();
	private float amountInsertToTable;
	private Beverage beverageInsertToTable;
	DefaultTableModel selectBeverageTableModel;
	
	/**
	 * Create the frame.
	 */
	private UIChoose() {
		addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent windowEvent) {
				dispose();
				UIMain.getUIMainInstance().setVisible(true);
			}
		});
		setBounds(100, 100, 678, 568);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(0, 204, 153));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		dimension = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(dimension.width/2-getSize().width/2, dimension.height/2-getSize().height/2);
		
		poweredPanel = new JPanel();
		poweredPanel.setBackground(new Color(0, 204, 153));
		
		label = new JLabel("powered by java");
		label.setFont(new Font("Tahoma", Font.BOLD, 12));
		GroupLayout gl_poweredPanel = new GroupLayout(poweredPanel);
		gl_poweredPanel.setHorizontalGroup(
			gl_poweredPanel.createParallelGroup(Alignment.LEADING)
				.addComponent(label, GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
		);
		gl_poweredPanel.setVerticalGroup(
			gl_poweredPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_poweredPanel.createSequentialGroup()
					.addComponent(label, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		poweredPanel.setLayout(gl_poweredPanel);
		
		creditPanel = new JPanel();
		creditPanel.setBackground(new Color(0, 204, 153));
		
		label_1 = new JLabel("Designed by");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		label_2 = new JLabel("Abram Lawendy");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setFont(new Font("Tahoma", Font.PLAIN, 12));
		GroupLayout gl_creditPanel = new GroupLayout(creditPanel);
		gl_creditPanel.setHorizontalGroup(
			gl_creditPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_creditPanel.createSequentialGroup()
					.addGroup(gl_creditPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1, GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_creditPanel.setVerticalGroup(
			gl_creditPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_creditPanel.createSequentialGroup()
					.addComponent(label_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(label_2)
					.addGap(12))
		);
		creditPanel.setLayout(gl_creditPanel);
		
		greetingPanel = new JPanel();
		greetingPanel.setBackground(new Color(0, 204, 153));
		
		selectionPanel = new JPanel();
		selectionPanel.setBackground(new Color(0, 204, 153));
		
		addPanel = new JPanel();
		addPanel.setBackground(new Color(0, 204, 153));
		
		displayBeveragePanel = new JPanel();
		displayBeveragePanel.setBackground(new Color(0, 204, 153));
		
		removePanel = new JPanel();
		removePanel.setBackground(new Color(0, 204, 153));
		
		removeButton = new JButton("REMOVE");
		removeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(selectedBeverageTable.getSelectedRow() != -1)
					selectBeverageTableModel.removeRow(selectedBeverageTable.getSelectedRow());
			}
		});

		removeButton.setForeground(Color.BLACK);
		removeButton.setFont(new Font("Calibri", Font.BOLD | Font.ITALIC, 17));
		removeButton.setBackground(new Color(0, 204, 153));
		GroupLayout gl_removePanel = new GroupLayout(removePanel);
		gl_removePanel.setHorizontalGroup(
			gl_removePanel.createParallelGroup(Alignment.LEADING)
				.addComponent(removeButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
		);
		gl_removePanel.setVerticalGroup(
			gl_removePanel.createParallelGroup(Alignment.LEADING)
				.addComponent(removeButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
		);
		removePanel.setLayout(gl_removePanel);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(displayBeveragePanel, GroupLayout.PREFERRED_SIZE, 418, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
							.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_contentPane.createSequentialGroup()
										.addComponent(addPanel, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(removePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addComponent(greetingPanel, GroupLayout.PREFERRED_SIZE, 300, GroupLayout.PREFERRED_SIZE)
									.addComponent(selectionPanel, GroupLayout.PREFERRED_SIZE, 546, GroupLayout.PREFERRED_SIZE))
								.addGap(220))
							.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
								.addComponent(poweredPanel, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(creditPanel, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
								.addGap(138))))
					.addGap(138))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(greetingPanel, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(selectionPanel, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(addPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(removePanel, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(displayBeveragePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
							.addComponent(poweredPanel, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
						.addComponent(creditPanel, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)))
		);
		
		selectedBeverageTable = new JTable();
		selectedBeverageTable.setShowHorizontalLines(false);
		selectedBeverageTable.setShowGrid(false);
		selectedBeverageTable.setFont(new Font("Calibri", Font.PLAIN, 15));
		selectedBeverageTable.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		selectedBeverageTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Beverage", "Amount", "Price"
			}
		) {
			/**
			 * 
			 */
			private static final long serialVersionUID = -6581139558601463459L;
			@SuppressWarnings("rawtypes")
			Class[] columnTypes = new Class[] {
				Object.class, Float.class, Float.class
			};
			public Class<?> getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		selectedBeverageTable.getColumnModel().getColumn(0).setPreferredWidth(150);
		
		txtBeverageAmountPrice = new JTextField();
		txtBeverageAmountPrice.setFont(new Font("Calibri", Font.BOLD, 16));
		txtBeverageAmountPrice.setBorder(new LineBorder(Color.BLACK, 1, true));
		txtBeverageAmountPrice.setText(" Beverage                           |Amount            |Price");
		txtBeverageAmountPrice.setColumns(10);
		GroupLayout gl_displayBeveragePanel = new GroupLayout(displayBeveragePanel);
		gl_displayBeveragePanel.setHorizontalGroup(
			gl_displayBeveragePanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_displayBeveragePanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_displayBeveragePanel.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, gl_displayBeveragePanel.createSequentialGroup()
							.addComponent(txtBeverageAmountPrice, GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
							.addGap(10))
						.addGroup(Alignment.TRAILING, gl_displayBeveragePanel.createSequentialGroup()
							.addComponent(selectedBeverageTable, GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
							.addContainerGap())))
		);
		gl_displayBeveragePanel.setVerticalGroup(
			gl_displayBeveragePanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_displayBeveragePanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(txtBeverageAmountPrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(selectedBeverageTable, GroupLayout.PREFERRED_SIZE, 263, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		displayBeveragePanel.setLayout(gl_displayBeveragePanel);

		addButton = new JButton("ADD");
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					amountInsertToTable = (float)Integer.parseInt(amountTextBox.getText());
					if(amountInsertToTable > 0 && amountInsertToTable <= 10){
						selectBeverageTableModel = (DefaultTableModel) selectedBeverageTable.getModel();
						beverageInsertToTable = (Beverage) beveragesComboBox.getSelectedItem();
						vectorInsertToTable.add(beverageInsertToTable.toString());
						vectorInsertToTable.add(amountInsertToTable);
						vectorInsertToTable.add((float) amountInsertToTable * beverageInsertToTable.getBeveragePrice());
						selectBeverageTableModel.addRow(vectorInsertToTable.toArray());
						amountInsertToTable = (float) 0.0;
						beverageInsertToTable = null;
						vectorInsertToTable.removeAllElements();
						}
				}
				catch(Exception E){
					E.printStackTrace();
				}
			}
		});
		addButton.setBackground(new Color(0, 204, 153));
		addButton.setFont(new Font("Calibri", Font.BOLD | Font.ITALIC, 17));
		addButton.setForeground(new Color(0, 0, 0));
		GroupLayout gl_addPanel = new GroupLayout(addPanel);
		gl_addPanel.setHorizontalGroup(
			gl_addPanel.createParallelGroup(Alignment.LEADING)
				.addComponent(addButton, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
		);
		gl_addPanel.setVerticalGroup(
			gl_addPanel.createParallelGroup(Alignment.LEADING)
				.addComponent(addButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
		);
		addPanel.setLayout(gl_addPanel);
		
		beveragesComboBox = new JComboBox<Beverage>();
		beveragesComboBox.setFont(new Font("Calibri", Font.PLAIN, 15));
		beveragesComboBox.addItem(new Beverage("Wasser 0.5L", (float) 1.234));
		beveragesComboBox.addItem(new Beverage("Krombacher 0.33L", (float) 1.06));
		beveragesComboBox.addItem(new Beverage("Jever 0.33L", (float) 1.006));
		//beveragesComboBox.setModel(new DefaultComboBoxModel(new String[] {new Beverage("Wassera 0.5L", (float) 1.99).getStringName(), new Beverage("Krombacher 0.33L", (float) 1.21).getStringName(), new Beverage("Jever 0.33L", (float) 1).getStringName()}));
		beveragesComboBox.setMaximumRowCount(4);
		beveragesComboBox.setBackground(new Color(0, 204, 153));
		
		amountTextBox = new JTextField();
		amountTextBox.setFont(new Font("Calibri", Font.PLAIN, 15));
		amountTextBox.setHorizontalAlignment(SwingConstants.CENTER);
		amountTextBox.setColumns(10);
		
		beverageLabel = new JLabel("Beverage");
		beverageLabel.setFont(new Font("Calibri", Font.BOLD | Font.ITALIC, 17));
		beverageLabel.setHorizontalAlignment(SwingConstants.CENTER);
		
		amountLabel = new JLabel("Amount");
		amountLabel.setFont(new Font("Calibri", Font.BOLD | Font.ITALIC, 17));
		amountLabel.setHorizontalAlignment(SwingConstants.CENTER);
		GroupLayout gl_selectionPanel = new GroupLayout(selectionPanel);
		gl_selectionPanel.setHorizontalGroup(
			gl_selectionPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_selectionPanel.createSequentialGroup()
					.addGroup(gl_selectionPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(beveragesComboBox, 0, 454, Short.MAX_VALUE)
						.addComponent(beverageLabel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE))
					.addGap(18)
					.addGroup(gl_selectionPanel.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(amountLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(amountTextBox, GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)))
		);
		gl_selectionPanel.setVerticalGroup(
			gl_selectionPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_selectionPanel.createSequentialGroup()
					.addGroup(gl_selectionPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(amountLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(beverageLabel, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_selectionPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(amountTextBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(beveragesComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		selectionPanel.setLayout(gl_selectionPanel);
		
		lblWelcome = new JLabel("Hello");
		lblWelcome.setToolTipText("");
		lblWelcome.setHorizontalAlignment(SwingConstants.CENTER);
		lblWelcome.setFont(new Font("Tahoma", Font.BOLD, 15));
		GroupLayout gl_greetingPanel = new GroupLayout(greetingPanel);
		gl_greetingPanel.setHorizontalGroup(
			gl_greetingPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_greetingPanel.createSequentialGroup()
					.addComponent(lblWelcome, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(162, Short.MAX_VALUE))
		);
		gl_greetingPanel.setVerticalGroup(
			gl_greetingPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_greetingPanel.createSequentialGroup()
					.addComponent(lblWelcome, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		greetingPanel.setLayout(gl_greetingPanel);
		contentPane.setLayout(gl_contentPane);
	}

	public static UIChoose getUIChooseInstance(){
		if(uiChooseInstance == null)
			uiChooseInstance = new UIChoose();
		return uiChooseInstance;
	}
}
